package com.loong.system.controller.backend;

import com.loong.common.domain.BaseQuery;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.system.common.backend.param.SystemConfigStatusParam;
import com.loong.system.common.backend.param.SystemConfigureParam;
import com.loong.system.common.backend.param.SystemConfigureQueryParam;
import com.loong.system.handler.SystemConfigureHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 配置控制器
 */
@RestController
@RequestMapping("/manage/system/configure")
public class SystemConfigureController extends BaseBackendController {
    @Resource
    private SystemConfigureHandler systemConfigureHandler;

    /**
     * 保存配置
     * @param param
     * @return
     */
    @PostMapping("save")
    public Result save(@Validated(value = {CommonInsert.class, CommonValidate.class})SystemConfigureParam param){
        param.setId(null);
        return handlerExecutor.execute(()->systemConfigureHandler.save(param));
    }

    /**
     * 更新配置
     * @param param
     * @return
     */
    @PostMapping("update")
    public Result update(@Validated(value = {CommonUpdate.class, CommonValidate.class}) SystemConfigureParam param){
        return  handlerExecutor.execute(()->systemConfigureHandler.update(param));
    }


    /**
     * 设置配置启用状态
     * @param statusParam 配置id 和 状态
     * @return
     */
    @PostMapping("setConfigureStatus")
    public Result setConfigureStatus(@Validated(value = {CommonUpdate.class, CommonValidate.class}) SystemConfigStatusParam statusParam){
        SystemConfigureParam param = SystemConfigureParam.builder().id(statusParam.getId()).configStatus(statusParam.getFlag()).build();
        return  handlerExecutor.execute(()->systemConfigureHandler.update(param));
    }


    /**
     *根据id删除配置
     * @param id
     */
    @PostMapping("delete")
    public Result deleteById(@RequestParam("id")  Long id){
        return  handlerExecutor.execute(()->systemConfigureHandler.deleteConfigureById(id));
    }

    /**
     * 分页查询
     * @param baseQuery
     * @param param
     * @return
     */
    @PostMapping("pageQuery")
    public Result pageQuery(BaseQuery baseQuery, SystemConfigureQueryParam param){
        return  handlerExecutor.execute(()->systemConfigureHandler.pageQuery(baseQuery,param));
    }



}
